package Dungeoneering.Events;

import org.powerbot.game.api.util.Timer;

/**
 * Every analysed most-efficient step will be molded into a DungeoneeringTask that we'll run in a queued system.
 * MAKE IT CASE-DRIVEN!!! This is so a task can get interupted to make way for a task with higher priority. The task will be re-added to the queue.
 * Fill in the initialise method well, because this will be run first. So make sure that it can be used as a re-initialise
 * @author Nicolaas
 *
 */
public class DungeoneeringTask {
	
	private enum TaskStatus { SETUP, WAITING, FINISHED };
	
	public int priority;
	private boolean finished;
	
	private TaskStatus status;
	
	private Timer t;

	public DungeoneeringTask(int priority) {
		this.priority = priority;
	}
	
	public int getPriority() {
		return priority;
	}
	
	public void changePriority(int priority) {
		System.out.println("Changing priority from " + this.priority + " to " + priority);
		this.priority = priority;
		this.finished = true;
		EventCaster.getInstance().dispatchEvent(this);
	}
	
	private void initialise() {
		this.finished = false;
		this.status = TaskStatus.SETUP;
		if (priority == 6)
			t = new Timer(5000);
		else
			t = new Timer(500);
	}
	
	private boolean isFinished() {
		return finished;
	}
	
	public boolean canBeDestroyed() {
		return status == TaskStatus.FINISHED;
	}
	
	public void interupt() {
		System.out.println("Interrupting a task with priority " + priority);
		finished = true;
	}
	
	public void process() {
		initialise();
		while (!isFinished())
			loop();
	}
	
	private void loop() {
		switch (status) {
		case SETUP:
			status = TaskStatus.WAITING;
			break;
		case WAITING:
			if (!t.isRunning()) {
				System.out.println("Processed an event with priority: " + priority);
				finished = true;
				status = TaskStatus.FINISHED; // this one is for real.
			}
			break;
		}
		
	}

}
